package com.xzw.jdbc;

import java.sql.*;
import java.util.Scanner;

public class AppLogin {
    public static void main(String[] args)throws Exception {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入账号: ");
        String username = scanner.nextLine();

        System.out.println("请输入密码: ");
        String password = scanner.nextLine();

        loginPlus(username,password);
    }

    public static void login(String n,String p)throws Exception {
        System.out.println("username="+n);
        System.out.println("password="+p);
        String sql="select id from `user` where username= '" + n +
                "' and 'password'=MD5('" + p +
                "')";
        System.out.println("组成的SQL语句是: "+sql);

        Class.forName("com.mysql.jdbc.Driver");
        String url=("jdbc:mysql://localhost:3306/xzw?useSSL=false");
        String username="root";
        String password="root";
        Connection connection = DriverManager.getConnection(url, username, password);
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        if(resultSet.next()){
            System.out.println("登录成功！");
        } else {
            System.out.println("登陆失败！");
        }
    }

    public static void loginPlus(String n, String p) throws Exception{
        System.out.println("请输入账号: "+ n);
        System.out.println("请输入密码: "+ p);
        String sql="select id from `user` where username=? and `password`=MD5(?)";
        Class.forName("com.mysql.jdbc.Driver");
        String url=("jdbc:mysql://localhost:3306/xzw?useSSL=false");
        String username="root";
        String password="root";
        Connection connection = DriverManager.getConnection(url, username, password);
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,n);
        ps.setString(2,p);
        ResultSet set = ps.executeQuery();
        if(set.next()){
            System.out.println("登录成功！");
        } else {
            System.out.println("登陆失败！");
        }
    }
}
